﻿using System;
using System.Collections.Generic;

namespace smp.ButterflyCakes.Models.ViewModels.Schedule
{
    public class DateModel
    {
        private readonly Dictionary<string, int> _months = new Dictionary<string, int>
                                                               {
                                                                   {"January", 1},
                                                                   {"February", 2},
                                                                   {"March", 3},
                                                                   {"April", 4},
                                                                   {"May", 5},
                                                                   {"June", 6},
                                                                   {"July", 7},
                                                                   {"August", 8},
                                                                   {"September", 9},
                                                                   {"October", 10},
                                                                   {"November", 11},
                                                                   {"December", 12},
                                                               };

        private readonly List<string> _monthList = new List<string>
                                                       {
                                                           "January",
                                                           "February",
                                                           "March",
                                                           "April",
                                                           "May",
                                                           "June",
                                                           "July",
                                                           "August",
                                                           "September",
                                                           "October",
                                                           "November",
                                                           "December"
                                                       };

        private readonly List<string> _yearLst = new List<string>();

        public DateModel() : this(DateTime.Today.ToString("dd-MMMM-yyyy"))
        {
        }

        public DateModel(string dateString)
        {
            if (string.IsNullOrEmpty(dateString))
                throw new ArgumentNullException("dateString");
            var dateParts = dateString.Split('-');
            if(dateParts.Length == 2)
            {
                SetMonth(dateParts[0]);
                SetYear(dateParts[1]);
                Day = 1;
            }
            else if (dateParts.Length == 3)
            {
                SetDay(dateParts[0]);
                SetMonth(dateParts[1]);
                SetYear(dateParts[2]);
            }
            else
            {
                throw new ArgumentException("Invalid dateString :" + dateString + " for Schedule");
            }
            for (var i = 2012; i < 2022; i++)
            {
                _yearLst.Add(i.ToString());
            }
        }
        
        public int Day { get; private set; }
        public int Month { get; private set; }
        public int Year { get; private set; }
        public DateTime Date { get { return new DateTime(Year, Month, Day); } }
        public string DisplayMonth { get { return Date.ToString("MMMM"); } }
        public string DisplayYear { get { return Date.ToString("yyyy"); } }
        public string DisplayDate { get { return Date.ToString("dd-MMMM-yyyy"); } }
        public List<string> MonthList { get { return _monthList; } }
        public List<string> YearList { get { return _yearLst; } }
        public DateTime StartOfMonth
        {
            get{return new DateTime(Year, Month, 1);}
        }

        public DateTime EndOfMonth
        {
            get { return new DateTime(Year, Month, 1).AddMonths(1).AddDays(-1); }
        }

        public DateModel NextMonth()
        {
            var newDate = Date.AddMonths(1);
            Day = newDate.Day;
            Month = newDate.Month;
            Year = newDate.Year;
            return this;
        }

        public DateModel PreviousMonth()
        {
            var newDate = Date.AddMonths(-1);
            Day = newDate.Day;
            Month = newDate.Month;
            Year = newDate.Year;
            return this;
        }

        private void SetMonth(string monthDatePart)
        {
            int monthValue;
            if (_months.ContainsKey(monthDatePart))
            {
                monthValue = _months[monthDatePart];
            }
            else
            {
                if (!int.TryParse(monthDatePart, out monthValue))
                    throw new ArgumentException("month value " + monthDatePart + " is an invalid month number");
            }
            Month = monthValue;
        }

        private void SetYear(string yearDatePart)
        {
            int yearValue;
            if (!int.TryParse(yearDatePart, out yearValue))
            {
                throw new ArgumentException("year value " + yearDatePart + " is an invalid year number");
            }
            Year = yearValue;
        }

        private void SetDay(string dayDatePart)
        {
            int dayValue;
            if (!int.TryParse(dayDatePart, out dayValue))
            {
                throw new ArgumentException("day value " + dayDatePart + " is an invalid day number");
            }
            Day = dayValue;
        }
    }
}
